package com.shanshan.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shanshan.system.entity.Notice;
import com.shanshan.system.mapper.NoticeMapper;
import com.shanshan.system.service.NoticeService;
import com.shanshan.system.vo.NoticeVo;
import com.shanshan.system.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author keluoyisi
 * @program: shanshan
 * @description:
 * @date 2023-02-08 21:59:49
 */
@Service
@SuppressWarnings("all")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Override
    public Result queryAllNotice(int currentPage, int pageSize, NoticeVo noticeVo) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        if(noticeVo.getDate()!=null){
            noticeVo.setStartDate(noticeVo.getDate()[0]);
            noticeVo.setEndDate(noticeVo.getDate()[1]);
            wrapper.between("time",noticeVo.getStartDate(),noticeVo.getEndDate());
        }

        if (noticeVo.getTitle()!=null){
            wrapper.like("title",noticeVo.getTitle());
        }
        PageHelper.startPage(currentPage,pageSize);
        List<Notice> list = noticeMapper.selectList(wrapper);
        PageInfo<Notice> pageInfo = new PageInfo<>(list);
        return new Result(200,"查询成功",pageInfo);
    }

    @Override
    public Result deleteById(int id) {
        int i = noticeMapper.deleteById(id);
        if (i==1){
            return new Result(200,"删除成功");
        }
        return new Result(500,"删除失败");
    }

    @Override
    public Result updateNotice(Notice notice) {
        int i = noticeMapper.updateById(notice);
        if (i==1){
            return new Result(200,"修改成功");
        }
        return new Result(500,"修改失败");
    }

    @Override
    public Result addNotice(Notice notice) {
        int insert = noticeMapper.insert(notice);
        if (insert==1){
            return new Result(200,"添加成功");
        }
        return new Result(500,"添加失败");
    }

    @Override
    public Result findAll() {
        //查询公告
        List<Notice> notices = noticeMapper.selectAllByUppid();
        return new Result(200,"查询成功",notices);
    }
}
